﻿#include<iostream>
#include<algorithm>
#include<vector>
#define TIME 2
#define NUM  3
//三个不同的时间片分配
#define FIRST 1
#define SECOND 2
#define THIRD 3
int mYclock = 0;  //计时器，模拟时间的流逝
using namespace std;
class Process
{
public:
	int m_id; //进程号
	int m_enter_time; //进入内存的时间
	int m_run_time;  //需要运行的时间
};
bool FCFS(Process p1, Process p2)
{
	return p1.m_enter_time < p2.m_enter_time;
}
void print(Process p)
{
	cout << "进程号：" << p.m_id << " " << "进入内存时间：" << p.m_enter_time << " " << "待运行时间" << p.m_run_time << endl;
}
//进程是否消耗完所有的运行时间
bool isFinish(Process& p)
{
	if (p.m_run_time - TIME > 0)
	{
		p.m_run_time -= TIME;
		return false;
	}
	else
	{
		return true;
	}

}
bool isFinish0(Process& p)
{
	if (p.m_run_time - FIRST > 0)
	{
		p.m_run_time -= FIRST;
		mYclock += FIRST;
		return false;
	}
	else
	{
		mYclock += p.m_run_time;
		return true;
	}

}
bool isFinish1(Process& p)
{
	if (p.m_run_time - SECOND > 0)
	{
		p.m_run_time -= SECOND;
		mYclock += SECOND;
		return false;
	}
	else
	{
		mYclock += p.m_run_time;
		return true;
	}

}
bool isFinish2(Process& p)
{
	if (p.m_run_time - THIRD > 0)
	{
		p.m_run_time -= THIRD;
		mYclock += THIRD;
		return false;
	}
	else
	{
		mYclock += p.m_run_time;
		return true;
	}

}
void InitQueue(vector<Process>& v)
{
	cout << "请输入进程个数！" << endl;
	int num;
	cin >> num;
	for (int i = 0; i < num; i++)
	{
		cout << i + 1 << "号进程" << endl;
		int id;
		id = i + 1;
		int enter_time;
		cout << "进入内存时间：" << " ";
		cin >> enter_time;
		cout << "待运行时间" << " ";
		int run_time;
		cin >> run_time;
		Process p;
		p.m_id = id;
		p.m_enter_time = enter_time;
		p.m_run_time = run_time;
		v.push_back(p);
		system("cls");
	}
}
void RR()
{
	//等待队列
	vector<Process> v;
	InitQueue(v);
	sort(v.begin(), v.end(), FCFS);
	while (!v.empty())
	{
		Process p(v.front());
		cout << "进程" << p.m_id << "进入处理机！" << endl;
		v.erase(v.begin());
		if (isFinish(p))
		{
			cout << "进程" << p.m_id << "完成全部运行！" << endl;
		}
		else
		{
			cout << "进程" << p.m_id << "进入就绪队列队尾" << endl;
			v.push_back(p);
		}
	}
}
void MFQS()
{
	//初始化各个进程即进程信息
	vector<Process> v;
	InitQueue(v);
	//初始化三个级别的就绪队列
	vector<vector<Process>> q;
	for (int i = 0; i < NUM; i++)
	{
		vector<Process> v;
		q.push_back(v);
	}
	//开始时间循环
	while (!q[0].empty() || !v.empty() || !q[1].empty() || !q[2].empty())
	{
		mYclock++; //时间刻度到达mYclock
		//达到进入时间的进程进入第0级队列
		if (!v.empty() && v.front().m_enter_time <= mYclock)
		{
			Process p(v.front());
			v.erase(v.begin());
			q[0].push_back(p);
		}
		//如果第零级队列不为空，则队首进程进处理机
		if (!q[0].empty())
		{
			Process p(q[0].front());
			cout << "进程" << p.m_id << "进入处理机！" << endl;
			q[0].erase(q[0].begin());
			if (isFinish0(p))
				cout << "进程" << p.m_id << "完成全部运行！" << endl;
			else
			{

				cout << "进程" << p.m_id << "进入下一级队列队尾" << endl;
				q[1].push_back(p);
			}
		}
		else//第0级进程为空
		{

			//考虑第1级进程
			if (!q[1].empty())//不为空，队首进处理机
			{
				Process p(q[1].front());
				cout << "进程" << p.m_id << "进入处理机！" << endl;
				q[1].erase(q[1].begin());
				if (isFinish1(p))
					cout << "进程" << p.m_id << "完成全部运行！" << endl;
				else
				{

					cout << "进程" << p.m_id << "进入下一级队列队尾" << endl;
					q[2].push_back(p);
				}
			}
			else//第1级进程为空，只剩下了第2级进程
			{
				if (q[2].empty() && v.empty())
				{
					cout << "完成所有进程的处理！" << endl;
				}
				if (!q[2].empty())
				{
					Process p(q[2].front());
					cout << "进程" << p.m_id << "进入处理机！" << endl;
					q[2].erase(q[2].begin());
					if (isFinish2(p))
						cout << "进程" << p.m_id << "完成全部运行！" << endl;
					else
					{

						cout << "进程" << p.m_id << "进入本级队列队尾" << endl;
						q[2].push_back(p);
					}
				}
			}
		}
	}
}
void menu()
{
	cout << "********进程调度模拟**********" << endl;
	cout << "1.简单循环轮转调度" << endl;
	cout << "2.多级反馈队列调度(非抢占式)" << endl;
	cout << "3.退出" << endl;
	cout << "********************************" << endl;
}
int main()
{
	menu();
	int select;
	cin >> select;
	switch (select)
	{
	case 1:
		RR();
		break;
	case 2:
		MFQS();
		break;
	case 3:
		exit(0);
	default:
		break;
	}
	MFQS();
	return 0;
}